<?php

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Exceptions\R;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Validator;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * 验证请求数据
     * @param       $params
     * @param       $rule
     * @param array $messages
     * @param array $attributes
     * @throws ApiException
     */
    public function validateRequest($params, $rule, $messages = [], $attributes = [])
    {
        $v = Validator::make($params, $rule, $messages, $attributes);
        if ($v->fails()) {
            throw new ApiException(R::INPUT_DATA_ERROR, $v->errors()->first());
        }
    }
}
